#include <iostream>
using namespace std;

/** Return this number if it is a single digit, otherwise, return the sum of the two digits */
int getDigit(int number)
{
  return number % 10 + (number / 10 % 10);
}

/** Return sum of odd place digits in number */
int sumOfOddPlace(const char * const cardNumber)
{
  int result = 0;

  for (int i = strlen(cardNumber) - 1; i >= 0; i = i - 2)
  {
    result += cardNumber[i] - '0';
  }

  return result;
}

/** Get the result from Step 2 */
int sumOfEvenPlace(const char * const cardNumber)
{
  int result = 0;

  for (int i = strlen(cardNumber) - 2; i >= 0; i = i - 2)
  {
    result += getDigit((cardNumber[i] - '0') * 2);
  }

  return result;
}

int main()
{
  cout << "Enter a credit card number as a string: ";
  char cardNumber[17]; // hold 16 characters with a null terminator
  cin >> cardNumber;

  if ((sumOfEvenPlace(cardNumber) + sumOfOddPlace(cardNumber)) % 10 == 0)
    cout << cardNumber << " is valid" << endl;
  else
    cout << cardNumber << " is invalid" << endl;

  return 0;
}
